package com.jsmile.cloud.accountcenter.shop.model;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.jsmile.cloud.accountcenter.api.constants.AccountConstants;
import com.jsmile.cloud.accountcenter.api.enums.ShopTypeEnum;
import com.jsmile.cloud.accountcenter.api.req.ReqMemberRegister;
import com.jsmile.cloud.accountcenter.shop.event.shop.ShopChangeNameEvent;
import com.jsmile.cloud.accountcenter.shop.event.shop.ShopChangePhoneEvent;
import com.jsmile.cloud.accountcenter.shop.event.shop.ShopRegisterEvent;
import com.jsmile.cloud.accountcenter.shop.repo.ShopSearchRepository;
import com.jsmile.mail.security.event.DomainEventPublisher;
import com.jsmile.mall.cache.kit.SpringKit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月04日 04:39:24
 */
@ApiModel
@Data
@TableName("t_ac_shop")
public class Shop extends Model<Shop> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    @ApiModelProperty(value = "账户id")
    private Long accountId;
    @ApiModelProperty(value = "用户id")
    private Long userId;
    @ApiModelProperty(value = "商户id")
    private Long merchantId;
    @ApiModelProperty(value = "商户id")
    private Long tenantId;
    @ApiModelProperty(value = "店铺编号")
    private String shopNo;
    @ApiModelProperty(value = "店铺名称")
    private String shopName;
    @ApiModelProperty(value = "经度")
    private String longitude;
    @ApiModelProperty(value = "纬度")
    private String latitude;
    @ApiModelProperty(value = "店铺层级，默认：0")
    private Integer level;
    @ApiModelProperty(value = "父门店id")
    private Long parentId;
    @ApiModelProperty(value = "门店类型，0：B端门店，1：C端门店")
    private Integer shopType;
    @ApiModelProperty(value = "审核状态，0：待审核，1：审核通过，2：审核失败")
    private Integer verifyStatus;
    @ApiModelProperty(value = "经营业务")
    private String business;
    @ApiModelProperty(value = "联系人")
    private String contactName;
    @ApiModelProperty(value = "联系电话")
    private String contactPhone;
    @ApiModelProperty(value = "手机号码")
    private String mobilePhone;
    @ApiModelProperty(value = "固定电话")
    private String telephone;
    @ApiModelProperty(value = "邮箱")
    private String email;
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    @ApiModelProperty(value = "修改时间")
    private Date updateTime;
    @ApiModelProperty(value = "0:正常，1:删除")
    private Integer delFlag;
    @ApiModelProperty(value = "创建者")
    private String createBy;
    @ApiModelProperty(value = "修改者")
    private String updateBy;
    @ApiModelProperty(value = "审核失败原因")
    private String reviewMsg;
    @ApiModelProperty(value = "身份证照片")
    private String idcardImage;
    @ApiModelProperty(value = "营业执照")
    private String businessLicenseImage;
    @ApiModelProperty(value = "是否是默认店铺，0-否 1-是")
    private Integer hasDefault;
    @ApiModelProperty(value = "店铺url")
    private String shopUrl;
    @ApiModelProperty(value = "备注")
    private String remark;
    @ApiModelProperty(value = "省")
    private String province;
    @ApiModelProperty(value = "市")
    private String city;
    @ApiModelProperty(value = "区")
    private String district;
    @ApiModelProperty(value = "区编码")
    private String areaCode;
    @ApiModelProperty(value = "详细地址")
    private String detailAddress;
    @ApiModelProperty(value = "1启用 0禁用")
    private Integer enable;
    @ApiModelProperty(value = "平台id")
    private Integer platformId;
    @ApiModelProperty(value = "业务id")
    private Integer bizCode;

    public static Shop register(ReqMemberRegister reqMemberRegister, Long accountId) {
        Shop shop = SpringKit.getBean(ShopSearchRepository.class).findOneByUserId(reqMemberRegister.getUserId());
        if (null == shop) {
            shop = BeanUtil.toBean(reqMemberRegister, Shop.class);
            shop.setCreateTime(new Date());
            shop.setId(IdUtil.getSnowflake(AccountConstants.SNOWFLAKE.workerId, AccountConstants.SNOWFLAKE.datacenterId).nextId());
            DomainEventPublisher.publish(new ShopRegisterEvent(shop));
        }
        shop.setUpdateTime(new Date());
        shop.setAccountId(accountId);
        shop.setShopName(reqMemberRegister.getName());
        shop.setMobilePhone(reqMemberRegister.getPhone());
        shop.setShopType(ShopTypeEnum.C.getValue());
        shop.setProvince(reqMemberRegister.getProvince());
        shop.setCity(reqMemberRegister.getCity());
        shop.setDistrict(reqMemberRegister.getDistrict());
        if (!shop.getShopName().equals(reqMemberRegister.getName())) {
            DomainEventPublisher.publish(new ShopChangeNameEvent(shop));
        }
        if (!StrUtil.equals(reqMemberRegister.getPhone(), shop.getMobilePhone())) {
            DomainEventPublisher.publish(new ShopChangePhoneEvent(shop));
        }
        return shop;
    }
}
